package com.yjc.blog.controller;


import com.yjc.blog.domain.bo.EBookDao;
import com.yjc.blog.domain.bo.EbookSave;
import com.yjc.blog.domain.vo.EBookVO;
import com.yjc.blog.domain.vo.PageVO;
import com.yjc.blog.service.IEbookService;
import com.yjc.blog.utils.R;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 电子书 前端控制器
 * </p>
 *
 * @author yjc
 * @since 2024-10-27
 */
@RestController
@RequestMapping("ebook")
public class EbookController {
    @Resource
    private IEbookService ebookService;

    @GetMapping("/list")
    public R<PageVO<EBookVO>> list(@Valid EBookDao bookDao){
        R<PageVO<EBookVO>> r = new R<>();
        if (bookDao.getName() == null && bookDao.getId() == null) {
            PageVO<EBookVO> list = ebookService.listAll();
            r.setContent(list);
        }
        else {
            PageVO<EBookVO> ebook = ebookService.getByDao(bookDao);
            r.setContent(ebook);
        }

        if (r.getContent() == null){
            r.setMessage("未找到如何电子书");
        }
        return r;
    }

    /**
     * 保存
     * @param save
     * @return
     */
    @PostMapping("/save")
    public R save(@RequestBody EbookSave save){
        ebookService.save(save);
        return new R();
    }

    /**
     *
     * 根据id删除
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable("id") Long id){
        ebookService.delete(id);
        return new R();
    }

}
